package cn.cqie.frame.service.impl;

import cn.cqie.frame.bo.query.QueryEvaluateTaskBo;
import cn.cqie.frame.entity.EvaluateTask;
import cn.cqie.frame.mapper.EvaluateTaskMapper;
import cn.cqie.frame.service.EvaluateTaskService;
import cn.cqie.frame.utils.MyPageHelper;
import cn.cqie.frame.vo.EvaluateTaskVo;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 评教任务 服务实现类
 * </p>
 *
 * @author XYHL
 * @since 2021-10-16
 */
@Service
public class EvaluateTaskServiceImpl extends ServiceImpl<EvaluateTaskMapper, EvaluateTask> implements EvaluateTaskService {
    @Autowired
    private  EvaluateTaskService evaluateTaskService;

    /**
     * 新增or更新评价任务
     * @param po
     * @return
     */
    @Override
    public Boolean saveOrUpdateEvaluateTask(EvaluateTask po) {

        boolean save = evaluateTaskService.saveOrUpdate(po);
        return save;
    }

    /**
     * 通过id删除对应的评价任务
     * @param evaluateTaskId
     * @return 布尔值
     */
    @Override
    public Boolean deleteEvaluateTask(Integer evaluateTaskId) {
        boolean b = evaluateTaskService.removeById(evaluateTaskId);
        return b;
    }

    /**
     *
     * @param bo
     * @return
     */
    @Override
    public List<EvaluateTaskVo> evaluateTaskInfoPage(QueryEvaluateTaskBo bo) {
        MyPageHelper.startPage(bo);
        LambdaQueryWrapper<EvaluateTask> lqw = new LambdaQueryWrapper<>();
        lqw.eq(bo.getKeywords()!=null,EvaluateTask::getName,bo.getKeywords());
        List<EvaluateTask> evaluateTasks = baseMapper.selectList(lqw);

        return entity2Vo(evaluateTasks);
    }
    private List<EvaluateTaskVo> entity2Vo(List<EvaluateTask> collection) {
        List<EvaluateTaskVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, EvaluateTaskVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<EvaluateTask> page = (Page<EvaluateTask>) collection;
            Page<EvaluateTaskVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page, pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

}
